package sng.modules.view
{
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import sng.modules.model.vo.LoginVO;
	import sng.modules.system.NameSpace;
	import sng.modules.system.SocialSystem;
	import sng.modules.view.components.Login;
	import sng.modules.view.components.hud.HUDmain;
	
	/**
	 * 
	 * @author leonliu
	 * 
	 */	
	public class LoginMediator  extends Mediator
	{
		//获取应用实例
		private var loginPanel:Login;
		private var main:Main;

		
		public function LoginMediator(app:Object)
		{
			super(NameSpace.LoginMediatorName,app);
			main=app as Main;
			loginPanel=new  Login();
			loginPanel.addEventListener(NameSpace.LoginEvent, onLogin);
			PopUpManager.addPopUp(loginPanel, main);
			PopUpManager.centerPopUp(loginPanel);
			
			//临时添加，不用每次登录输入
			loginPanel.loginName.text = "test";
			loginPanel.loginPassword.text = "test";

		}
		
		//监听通知
		override public function listNotificationInterests():Array
		{
			var arr:Array=new Array();
			arr.push(NameSpace.LoginSuccess);
			arr.push(NameSpace.LoginFailed);
			return arr;
		}
		
		//响应通知
		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case NameSpace.LoginSuccess:
					setLoginSuc();
					break;
				case NameSpace.LoginFailed:
					setLoginFailed();
					break;
				default:
					break;
			}
		}
		
		//登录成功，移除登录窗口
		private function setLoginSuc():void
		{
			PopUpManager.removePopUp(loginPanel);
		}
		
		//登录失败，提示
		private function setLoginFailed():void
		{
			loginPanel.labStatus.text="用户名密码有误，请重试";
			loginPanel.loginName.setFocus();
		}
	
		private function onLogin(evt:Event):void
		{
			if (loginPanel.loginName.text != "" && loginPanel.loginPassword.text != "")
			{
				var loginVO:LoginVO=new LoginVO();
				loginVO.username=loginPanel.loginName.text;
				loginVO.password=loginPanel.loginPassword.text;
				sendNotification(NameSpace.ApplicationLogin, loginVO);
			}
			else
			{
				loginPanel.labStatus.text="请输入用户名和密码";
			}
		}

	}
}